package com.zhihuichongcheng.application.ui.openclass.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.smartlib.cmnObject.recycleView.OnItemOnclick;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.bean.openclass.OpenClassBean;
import com.zhihuichongcheng.application.bean.openclass.SubCategory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 3water on 2016/10/29.
 */

public class OpenCLassAdapter extends RecyclerView.Adapter<OpenCLassAdapter.ViewHold> {

    private Context mContext;
    private List<OpenClassBean> mListData = new ArrayList<>();
    private OnItemOnclick<OpenClassBean> onItemOnclick;
    public OpenCLassAdapter(Context context)
    {
        this.mContext = context;
    }

    public void setOnItemOnclick(OnItemOnclick<OpenClassBean> onItemOnclick)
    {
        this.onItemOnclick = onItemOnclick;
    }
    public void setData(List<OpenClassBean> list)
    {
        this.mListData.addAll(list);
    }

    @Override
    public ViewHold onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.listadapter_openclass,null);
        return new ViewHold(view);
    }

    @Override
    public void onBindViewHolder(ViewHold holder, final int position) {
        final OpenClassBean bean = mListData.get(position);
        holder.mTvTitle.setText(bean.getTitle());
        Glide.with(mContext).load(bean.getLogoUrl()).placeholder(R.drawable.ic_cricle_defalut_avatar).into(holder.mImavatar);
        holder.mTvStudyNum.setText("最近"+bean.getVisitCount()+"学习");
        SubCategory category = bean.getOpenClassCategory();
        if(category!=null)
        {
            holder.mTvType.setText(category.getCategoryName()+" "+bean.getContentType());
        }
        else
        {
            holder.mTvType.setText(bean.getContentType());
        }

        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemOnclick!=null)
                {
                    onItemOnclick.onClick(bean,position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mListData.size();
    }

    public void clear() {
        mListData.clear();
    }

    class ViewHold extends RecyclerView.ViewHolder{
        private View view;
        private ImageView mImavatar;
        private TextView mTvTitle,mTvType,mTvStudyNum;
        public ViewHold(View itemView) {
            super(itemView);
            view = itemView;
            mImavatar = (ImageView)itemView.findViewById(R.id.im_avatar);
            mTvTitle = (TextView)itemView.findViewById(R.id.tv_title);
            mTvType = (TextView)itemView.findViewById(R.id.tv_type);
            mTvStudyNum = (TextView)itemView.findViewById(R.id.tv_study_num);

        }
    }
}
